package com.fujitsu.hope.web.server;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.fujitsu.hope.web.client.bean.Irc;

class IrcMessageDao {
	private static final String JDBC_PATH = "org.sqlite.JDBC"; 
	private String fileName = "irc.db";
	private String dbPath = null;
	private Connection connection;
	private PreparedStatement insertStatement = null;
	private PreparedStatement getAllStatement = null;
	
	public IrcMessageDao(){
		this(null);
	}
	
	public IrcMessageDao(String dbPath){
		this.dbPath = dbPath;
		try {
			initConnection();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}
	
	private void initConnection()throws ClassNotFoundException, SQLException{
		Class.forName(JDBC_PATH);
		connection = DriverManager.getConnection("jdbc:sqlite:"+localPath());
	}
	
	private String localPath(){
		if(dbPath != null){
			return dbPath + System.getProperty("file.separator") + fileName;
		}
		return IrcMessageDao.class.getResource(fileName).getPath();
	}
	
	void create() throws SQLException{
		Statement statement = connection.createStatement();
		String sql = "create table irc (id integer primary key, channel, nickname, message, time)";
		statement.execute(sql);
	}
	
	void drop() throws SQLException{
		Statement statement = connection.createStatement();
		String sql = "drop table if exists irc ";
		statement.execute(sql);
	}
	
	void put(Irc message) throws SQLException{
		insert(message);
	}
	
	List<Irc> getAll() throws SQLException{
		if(getAllStatement == null)
			getAllStatement = getAllStatement();
		ResultSet resultSet = getAllStatement.executeQuery();
		List<Irc> result = new ArrayList<Irc>();
		while(resultSet.next()){
			Irc bean = new Irc(); 
			bean.setId(resultSet.getLong(1));
			bean.setNickname(resultSet.getString(2));
			bean.setChannel(resultSet.getString(3));
			bean.setMessage(resultSet.getString(4));
			bean.setTime(new Date(resultSet.getLong(5)));
			result.add(bean);
		}
		return result;
	}

	PreparedStatement getAllStatement() throws SQLException{
		String sql = "select id, nickname, channel, message, time from irc";
		return connection.prepareStatement(sql);
	}
	
	synchronized void insert(Irc message) throws SQLException{
		if(insertStatement == null) 
			insertStatement = insertStatement();
		insertStatement.setString(1, message.getNickname());
		insertStatement.setString(2, message.getChannel());
		insertStatement.setString(3, message.getMessage());
		insertStatement.setDate(4, new Date(message.getTime().getTime()));
		insertStatement.executeUpdate();
	}
	
	PreparedStatement insertStatement() throws SQLException{
		String sql = "insert into irc (nickname, channel, message, time) values (?, ?, ?, ?)";
		return connection.prepareStatement(sql);
	}
}